package com.youjiang.service.impl;

import com.youjiang.dao.SubjectMapper;
import com.youjiang.dao.entity.Subject;
import com.youjiang.dao.entity.SubjectExample;
import com.youjiang.service.ISubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yujia on 2017/11/17.
 */
@Service
public class SubjectService implements ISubjectService {
    @Autowired
    private SubjectMapper subjectMapper;

    public void insert() {
//        Subject subject = new Subject();
//        String str = "一级科目22ss";
//        subject.setSubL1(str);
//        subject.setSubL2(str);
//        int i = subjectMapper.insert(subject);
//        System.out.println(i);
    }

    public Subject query() {
        Subject subject =  subjectMapper.selectByPrimaryKey(1);
        if(null != subject){
            System.out.println(subject.getSubL1());
            System.out.println(subject.getSubL2());
        }
        return subject;
    }

    public List<String> getLevel1Subjects() {
        SubjectExample subjectExample = new SubjectExample();
        subjectExample.setDistinct(true);
        List<Subject> subjectList = subjectMapper.selectByExample(subjectExample);
        List<String> resultList = new ArrayList<String>();
        for (Subject subject:subjectList) {
            if(subject.getSubL1()!=null && !resultList.contains(subject.getSubL1())){
                resultList.add(subject.getSubL1());
            }
        }
        return resultList;
    }

    public List<String> getLevel2Subjects(String subL1) {
        SubjectExample subjectExample = new SubjectExample();
//        SubjectExample.Criteria criteria = subjectExample.createCriteria();
        subjectExample.setDistinct(true);
//        criteria.andSubL1EqualTo(subL1);
        subjectExample.or().andSubL1EqualTo(subL1);
        List<Subject> subjectList = subjectMapper.selectByExample(subjectExample);
        List<String> resultList = new ArrayList<String>();
        for (Subject subject:subjectList) {
            if(subject.getSubL2()!=null && !resultList.contains(subject.getSubL2())){
                resultList.add(subject.getSubL2());
            }
        }
        return resultList;
    }
}
